import {File} from 'better-xlsx';
import {saveAs} from 'file-saver';

function ExportExcel(column, dataSource, fileName = 'example', titleName) {
    // 新建工作谱
    const file = new File();
    // 新建表
    let sheet = file.addSheet('sheet-test');
    // 获取表头行数
    let depth = getDepth(column);
    // 获取表头的列数
    let columnNum = getColumns(column);
    // 新建表头行数
    let rowArr = [];
    for (let k = 0; k < depth; k++) {
        rowArr.push(sheet.addRow());
    }

    // 根据列数填充单元格
    rowArr.map(ele => {
        for (let j = 0; j < columnNum; j++) {
            let cell = ele.addCell();
            cell.value = j;
        }
    });
    // 初始化表头
    // 增加标题行
    let cellTitle = sheet.cell(0, 0);
    let rowTitle = sheet.row(0);
    rowTitle.setHeightCM(2);
    cellTitle.value = titleName;
    cellTitle.hMerge = column.length - 1;
    cellTitle.style.font.name = 'Arial';
    cellTitle.style.font.size = '20';
    cellTitle.style.font.bold = true;
    cellTitle.style.align.h = 'center';
    cellTitle.style.align.v = 'center';
    init(column, 1, 0);
    // 按顺序展平column
    let columnLineArr = [];
    columnLine(column);
    // 根据column,将dataSource里面的数据排序，并且转化为二维数组
    let dataSourceArr = [];
    for (var x = 0; x < dataSource.length; x++) {
        for (let info in dataSource[x]) {
            //此处可对导出的table表格数据做数据操作处理
            /*
            if(info.indexOf('IsActive_')>=0){
              console.log(dataSource[x][info])
              dataSource[x][info]=dataSource[x][info]?'中标':'';
              console.log(dataSource[x]);
            }
            */
        }
    }
    dataSource.map(ele => {
        let dataTemp = [];
        columnLineArr.map(item => {
            dataTemp.push({
                [item.dataIndex]: ele[item.dataIndex],
                value: ele[item.dataIndex],
            });
        });
        dataSourceArr.push(dataTemp);
    });
    // debugger;
    // 绘画表格数据
    dataSourceArr.forEach((item, index) => {
        //根据数据,创建对应个数的行
        let row = sheet.addRow();
        if (item[0].value !== '合计') {
            //创建对应个数的单元格
            item.map(ele => {
                let cell = row.addCell();
                if (ele.hasOwnProperty('num')) {
                    cell.value = index + 1;
                } else {
                    cell.value = ele.value;
                }
                cell.style.align.v = 'center';
                cell.style.align.h = 'center';
                cell.style.font.name = 'Arial';
                cell.style.font.size = '11';
                cell.style.border.top = 'thin';
                cell.style.border.bottom = 'thin';
                cell.style.border.left = 'thin';
                cell.style.border.right = 'thin';
                cell.style.border.topColor = '000000';
                cell.style.border.bottomColor = '000000';
                cell.style.border.leftColor = '000000';
                cell.style.border.rightColor = '000000';
            });
        } else {
            //创建对应个数的单元格
            item.map(ele => {
                let cell = row.addCell();
                if (ele.hasOwnProperty('num')) {
                    cell.value = index + 1;
                } else {
                    cell.value = ele.value;
                }
                cell.style.fill.fgColor = 'c9daf8';
                cell.style.fill.patternType = 'solid';
                cell.style.align.v = 'center';
                cell.style.align.h = 'center';
                cell.style.font.name = 'Arial';
                cell.style.font.size = '11';
                cell.style.border.top = 'thin';
                cell.style.border.bottom = 'thin';
                cell.style.border.left = 'thin';
                cell.style.border.right = 'thin';
                cell.style.border.topColor = '000000';
                cell.style.border.bottomColor = '000000';
                cell.style.border.leftColor = '000000';
                cell.style.border.rightColor = '000000';
            });
        }
        // row.setHeightCM(0.8);
    });
    //设置每列的宽度
    for (var i = 0; i < 4; i++) {
        sheet.col(i).width = 20;
    }
    file.saveAs('blob').then(function (content) {
        saveAs(content, fileName + '.xlsx');
    });

    // 按顺序展平column
    function columnLine(column) {
        column.map(ele => {
            if (ele.children === null || ele.children.length === 0) {
                columnLineArr.push(ele);
            } else {
                columnLine(ele.children);
            }
        });
    }

    // 初始化表头
    function init(column, rowIndex, columnIndex) {
        column.map((item, index) => {
            let hCell = sheet.cell(rowIndex, columnIndex);
            let row = sheet.row(rowIndex);
            // 如果没有子元素, 撑满列
            if (item.title === '操作') {
                hCell.value = '';
                return;
            } else if (item.children === null || item.children.length === 0) {
                row.setHeightCM(1);
                // 第一行加一个单元格
                hCell.style.applyAlignment = true;
                hCell.value = item.title;
                hCell.style.font.name = 'Arial';
                hCell.style.font.size = '11';
                hCell.vMerge = depth - rowIndex;
                hCell.style.align.h = 'center';
                hCell.style.align.v = 'center';
                hCell.style.font.color = 'FFFFFFFF';
                // 单元格背景颜色
                hCell.style.fill.fgColor = '5a7fee';
                hCell.style.fill.patternType = 'solid';
                // 单元格边框线
                hCell.style.border.top = 'thin';
                hCell.style.border.bottom = 'thin';
                hCell.style.border.left = 'thin';
                hCell.style.border.right = 'thin';
                hCell.style.border.topColor = 'FFFFFFFF';
                hCell.style.border.bottomColor = 'FFFFFFFF';
                hCell.style.border.leftColor = 'FFFFFFFF';
                hCell.style.border.rightColor = 'FFFFFFFF';
                columnIndex++;
                // rowIndex++
            } else {
                row.setHeightCM(1);
                let childrenNum = 0;

                function getColumns(arr) {
                    arr.map(ele => {
                        if (ele.children) {
                            getColumns(ele.children);
                        } else {
                            childrenNum++;
                        }
                    });
                }

                getColumns(item.children);
                hCell.hMerge = childrenNum - 1;
                hCell.value = item.title;
                hCell.style.applyAlignment = true;
                hCell.style.align.h = 'center';
                hCell.style.align.v = 'center';
                // if (hCell.value.includes('计量账')) {
                //     hCell.style.font.bold = true;
                //     hCell.style.font.size = '30';
                // } else if (hCell.value.includes('：')) {
                //     hCell.style.font.bold = true;
                //     hCell.style.font.size = '15';
                // } else {
                hCell.style.font.family = 'Arial';
                hCell.style.font.size = '11';
                hCell.style.font.color = 'FFFFFFFF';
                // 单元格背景颜色
                hCell.style.fill.fgColor = '5a7fee';
                hCell.style.fill.patternType = 'solid';
                // 单元格边框线
                hCell.style.border.top = 'thin';
                hCell.style.border.bottom = 'thin';
                hCell.style.border.left = 'thin';
                hCell.style.border.right = 'thin';
                hCell.style.border.topColor = 'FFFFFFFF';
                hCell.style.border.bottomColor = 'FFFFFFFF';
                hCell.style.border.leftColor = 'FFFFFFFF';
                hCell.style.border.rightColor = 'FFFFFFFF';
                // }
                let rowCopy = rowIndex;
                rowCopy++;
                init(item.children, rowCopy, columnIndex);
                // 下次单元格起点
                columnIndex = columnIndex + childrenNum;
            }
        });
        cellTitle.hMerge = columnIndex - 1;
    }

    // 获取表头rows
    function getDepth(arr) {
        const eleDepths = [];
        arr.forEach(ele => {
            let depth = 0;
            if (Array.isArray(ele.children)) {
                depth = getDepth(ele.children);
            }
            eleDepths.push(depth);
        });
        return 1 + max(eleDepths);
    }

    function max(arr) {
        return arr.reduce((accu, curr) => {
            if (curr > accu) return curr;
            return accu;
        });
    }

    // 计算表头列数
    function getColumns(arr) {
        let columnNum = 0;
        arr.map(ele => {
            if (ele.children) {
                getColumns(ele.children);
            } else {
                columnNum++;
            }
        });
        return columnNum;
    }
}

export default ExportExcel;

